package com.tl.app.service.impl;

import java.util.List;
import com.tl.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.tl.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.tl.app.domain.AppAccountLog;
import com.tl.app.mapper.AppAccountMapper;
import com.tl.app.domain.AppAccount;
import com.tl.app.service.IAppAccountService;

/**
 * 用户账户Service业务层处理
 * 
 * @author suhp
 * @date 2024-07-07
 */
@Service
public class AppAccountServiceImpl implements IAppAccountService 
{
    @Autowired
    private AppAccountMapper appAccountMapper;

    /**
     * 查询用户账户
     * 
     * @param id 用户账户主键
     * @return 用户账户
     */
    @Override
    public AppAccount selectAppAccountById(Long id)
    {
        return appAccountMapper.selectAppAccountById(id);
    }

    /**
     * 查询用户账户列表
     * 
     * @param appAccount 用户账户
     * @return 用户账户
     */
    @Override
    public List<AppAccount> selectAppAccountList(AppAccount appAccount)
    {
        return appAccountMapper.selectAppAccountList(appAccount);
    }

    /**
     * 新增用户账户
     * 
     * @param appAccount 用户账户
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAppAccount(AppAccount appAccount)
    {
        appAccount.setCreateTime(DateUtils.getNowDate());
        int rows = appAccountMapper.insertAppAccount(appAccount);
        insertAppAccountLog(appAccount);
        return rows;
    }

    /**
     * 修改用户账户
     * 
     * @param appAccount 用户账户
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAppAccount(AppAccount appAccount)
    {
        appAccountMapper.deleteAppAccountLogByUserId(appAccount.getId());
        insertAppAccountLog(appAccount);
        return appAccountMapper.updateAppAccount(appAccount);
    }

    /**
     * 批量删除用户账户
     * 
     * @param ids 需要删除的用户账户主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAppAccountByIds(Long[] ids)
    {
        appAccountMapper.deleteAppAccountLogByUserIds(ids);
        return appAccountMapper.deleteAppAccountByIds(ids);
    }

    /**
     * 删除用户账户信息
     * 
     * @param id 用户账户主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAppAccountById(Long id)
    {
        appAccountMapper.deleteAppAccountLogByUserId(id);
        return appAccountMapper.deleteAppAccountById(id);
    }

    /**
     * 新增用户流水信息
     * 
     * @param appAccount 用户账户对象
     */
    public void insertAppAccountLog(AppAccount appAccount)
    {
        List<AppAccountLog> appAccountLogList = appAccount.getAppAccountLogList();
        Long id = appAccount.getId();
        if (StringUtils.isNotNull(appAccountLogList))
        {
            List<AppAccountLog> list = new ArrayList<AppAccountLog>();
            for (AppAccountLog appAccountLog : appAccountLogList)
            {
                appAccountLog.setUserId(id);
                list.add(appAccountLog);
            }
            if (list.size() > 0)
            {
                appAccountMapper.batchAppAccountLog(list);
            }
        }
    }
}
